package com.etpjava.action;

import java.util.List;

import com.etpjava.dao.impl.EmployeeDaoImpl;
import com.etpjava.model.Employee;
import com.etpjava.service.EmployeeService;
import com.etpjava.util.GetSth;
import com.etpjava.util.Page;
import com.opensymphony.xwork2.ActionSupport;

/**
 * HrManAction
 * @author liuk
 * @version 0.1.1
 */
public class HrManAction extends ActionSupport {
	
	private static final long serialVersionUID = 2495033449345977427L;
	private Employee employee;
	private List<Employee> employees;
	private Integer id;

	// s* is for search
	private String sname;
	// 电话
	private String sphone;

	// 删除多个id
	private int[] ids;
	// 页码
	private int pagenum;

	private Page pagelist;


	private	EmployeeService emsev;

	public EmployeeService getEmsev() {
		return emsev;
	}

	public void setEmsev(EmployeeService emsev) {
		this.emsev = emsev;
	}

	// 增加或者修改
	public String add() {
		if (null == id) {
			if (!emsev.add(employee)) {
				return "error";
			}
		} else {
			emsev.modify(employee);
		}
		return "eminfo";
	}

	// 删除
	public String delete() {
		emsev.delete(id);
		return "list";
	}

	public String deletese() {
		if (ids != null && ids.length != 0) {
			for (int id : ids) {
				emsev.delete(id);
			}
		}
		return "list";
	}

	// 分页显示所有
	public String emPageList() {
		if (null == pagelist) {
			pagelist = new Page(1);
		}
		pagelist.setActionname("emPageList");
		new EmployeeDaoImpl().findAll(pagelist);
		if (pagelist.getList().size() == 0) {
			return "noresult";
		}
		return "list";
	}

	
	// 根据培训id查找em
	// public String findByTrid(){
	// Train train=emsev.findById(id);
	// employees=new ArrayList<Employee>(train.getMyEmployees());
	// if (null==employee) {
	// return "noresult";
	// }
	// return "list";
	// }

	// 通过id查找
	public String searchById() {
		if (null==id) {
			return "noresult";
		}
		
		employee = emsev.findById(id);
		if (null == employee) {
			return "noresult";
		}
		return "eminfo";
	}
	
	public String showem() {
		employee = emsev.findById(id);
		return "eminfo";
	}
	
	public String edit() {
		employee = emsev.findById(id);
		return "eminput";
	}
	
	// 通过name查找 已分页
	public String searchByName() {
		if (null == pagelist) {
			pagelist = new Page(1);
		}
		pagelist.setActionname("searchByName");

		emsev.findByName(pagelist, GetSth.change(sname));
		if (pagelist.getList().size() == 0) {
			return "noresult";
		}
		return "list";
	}

	// 通过电话号码查找，结果为第一条，其余不用
	public String searchByPhone() {
		List<Employee> ems = emsev.findByPhone(sphone);
		if (ems.size() == 0) {
			return "noresult";
		}
		employee = ems.get(0);
		return "eminfo";
	}

	public Employee getEmployee() {
		return employee;
	}

	public List<Employee> getEmployees() {
		return employees;
	}

	public String getSphone() {
		return sphone;
	}

	public Integer getId() {
		return id;
	}

	public int[] getIds() {
		return ids;
	}

	public Page getPagelist() {
		return pagelist;
	}

	public int getPagenum() {
		return pagenum;
	}

	public String getSname() {
		return sname;
	}

	public void setEmployee(Employee employee) {
		this.employee = employee;
	}

	public void setEmployees(List<Employee> employees) {
		this.employees = employees;
	}

	public void setId(Integer id) {
		this.id = id;
	}

	public void setIds(int[] ids) {
		this.ids = ids;
	}

	public void setPagelist(Page pagelist) {
		this.pagelist = pagelist;
	}

	public void setPagenum(int pagenum) {
		this.pagenum = pagenum;
	}

	public void setSname(String sname) {
		this.sname = sname;
	}

	public void setSphone(String sphone) {
		this.sphone = sphone;
	}


}
